import { routerRedux } from 'dva/router';
import { getCookie } from '../utils/auth';
import { login, logOutIns } from "../services/login"
import { getUserMsg, getUserInfo, getUserMenu } from '../services/auth'
import { removeCookie } from "../utils/auth"
import tabConfig from '../menuTab'
export default {
    namespace: 'auth',
    state: {
        locationWhere: true,
        userInformation: {
            Info:JSON.parse(localStorage.getItem("info")) || {},
            Msg:JSON.parse(localStorage.getItem("msg")) || {}
        },
        menu: [],
        now:{},
        where: [],
        appNav:0
    },
    subscriptions: {
        listenHistory({ dispatch, history }, select) {
            return history.listen((location) => {
                dispatch({ type: 'updataHrefInfo', payload: location })
                dispatch({ type: 'locationWhereFun', payload: { hidden: location.pathname == "/login" || location.pathname == "/404" } })
                // Expired verification
                if (!getCookie()) {
                    if (location.pathname != "/login") {
                        dispatch({ type: 'locationWhereFun', payload: { hidden: true } })
                    }
                }
            });
        },
    },
    effects: {
        // 路由跳转
        * redirect({ payload }, { put }) {
            // 非push为hash
            yield put(routerRedux.replace(payload.url));
        },
        *logOut({ payload }, { call, put }) {
            yield logOutIns(payload);
            yield removeCookie();
            localStorage.clear();
            yield put(routerRedux.replace('/login'));
          },
        // get userInfo
        *getUserInfoIsModule({ payload }, { select }) {
            return yield select(state => state.auth.userInformation)
        },
    },
    reducers: {
        updataAppNav(state, { payload }) {
            state.appNav = payload;
            return {
                ...state,
                ...payload,
            }
        },
        updataWhereInfo(state, { payload }) {
            state.where = payload;
            return {
                ...state,
                ...payload,
            }
        },
        updataHrefInfo(state, { payload }) {
            state.now = payload;
            return {
                ...state,
                ...payload,
            }
        },
        updataUserInfo(state, { payload }) {
            state.userInformation = payload;
            return {
                ...state,
                ...payload,
            }
        },
        locationWhereFun(state, { payload }) {
            state.locationWhere = payload;
            return {
                ...state,
                ...payload,
            }
        },
        updateMenus(state, { payload }) {
            state.menu = payload;
            return {
                ...state,
                ...payload,
            }
        },
    }

};